package cn.jiyun.service.impl;

import com.jiyun.common.pojo.product.CarOrder;
import com.jiyun.common.pojo.product.PackageMiddleOrder;
import cn.jiyun.mapper.CarOrderMapper;
import cn.jiyun.mapper.PackageMapper;
import cn.jiyun.mapper.PackageMiddleOrderMapper;
import cn.jiyun.service.PackageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jiyun.common.pojo.product.Package;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PackageServiceImpl implements PackageService {
    @Autowired
    private PackageMapper packageMapper;

    @Autowired
    private PackageMiddleOrderMapper packageMiddleOrderMapper;
    @Autowired
    private CarOrderMapper carOrderMapper;

    @Override
    public List<Package> findPackageList(Integer sortId, String search) {
        // 查询套餐
        List<Package> packageList = packageMapper.findPackageList(sortId, search);

        // 查询套餐和商品中间表，并以套餐ID为键，存储对应的中间表记录列表
        List<PackageMiddleOrder> packageMiddleOrderList = packageMiddleOrderMapper.findPackageMiddleOrderList();
        // 对套餐和商品中间表记录进行分组
        Map<Integer, List<PackageMiddleOrder>> packageMiddleOrderMap = groupPackageMiddleOrders(packageMiddleOrderList);

        // 查询商品，并以商品ID为键，存储对应的商品记录
        List<CarOrder> carOrders = carOrderMapper.findOrderList(null, null, null);
        // 存储商品记录
        Map<Integer, CarOrder> carOrderMap = mapCarOrders(carOrders);
        //并将中间表id存入carOrders中的mid里
        processMiddleOrders(packageMiddleOrderList, packageList, carOrders);

        // 遍历套餐列表，根据中间表和商品记录设置套餐对应的商品列表
        packageList.forEach(item -> {
            // 根据套餐ID获取关联的商品记录
            List<CarOrder> carOrderList = packageMiddleOrderMap.getOrDefault(item.getId(), new ArrayList<>())
                    .stream()
                    .map(PackageMiddleOrder::getOrderId)
                    .map(carOrderMap::get)
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());
            item.setCarOrders(carOrderList);
        });

        return packageList;
    }

    private Map<Integer, List<PackageMiddleOrder>> groupPackageMiddleOrders(List<PackageMiddleOrder> packageMiddleOrderList) {
        // 对套餐和商品中间表记录进行分组
        return packageMiddleOrderList.stream()
                .collect(Collectors.groupingBy(PackageMiddleOrder::getPackageId));
    }

    private Map<Integer, CarOrder> mapCarOrders(List<CarOrder> carOrders) {
        // 存储商品记录
        return carOrders.stream()
                .collect(Collectors.toMap(CarOrder::getId, Function.identity()));
    }



    //设置中间表的id，以便后面更换商品
    private void processMiddleOrders(List<PackageMiddleOrder> packageMiddleOrderList,
                                     List<Package> packageList,
                                     List<CarOrder> carOrders) {
        packageMiddleOrderList.forEach(item -> {
            packageList.forEach(packageItem -> {
                if (item.getPackageId() == packageItem.getId()) {
                    carOrders.forEach(carOrderItem -> {
                        if (item.getOrderId() == carOrderItem.getId()) {
                            carOrderItem.setMid(item.getId());
                        }
                    });
                }
            });
        });
    }


}
